package com.ruicar.afs.cloud.basic.common.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.basic.common.condition.BasicInsuranceInfoCondition;
import com.ruicar.afs.cloud.basic.common.condition.InsuranceRenewInfo;
import com.ruicar.afs.cloud.basic.common.entity.BasicInsuranceInfo;
import com.ruicar.afs.cloud.basic.common.service.BasicInsuranceInfoService;
import com.ruicar.afs.cloud.basic.common.vo.BasicInsuranceInfoVo;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.contract.enums.ContractStatusEnum;
import com.ruicar.afs.cloud.common.modules.contract.enums.InsuranceTypeEnum;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author xudongdong
 * @date 2020-06-08
 */

@RestController
@AllArgsConstructor
@RequestMapping("/custInsuranceInfo")
public class BasicInsuranceInfoController {

    private final BasicInsuranceInfoService basicInsuranceInfoService;

    @PostMapping("/getInsurancePopList")
    @ApiOperation(value = "获取资产保险信息数据 多条件")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<IPage<BasicInsuranceInfoVo>> getInsurancePopList(@RequestBody BasicInsuranceInfoCondition condition) {
        condition.setSysDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        condition.setContractStatus(ContractStatusEnum.contractEffective);
        Page page = new Page(condition.getPageNumber(), condition.getPageSize());
        IPage<List<BasicInsuranceInfoVo>> list = basicInsuranceInfoService.getInsurancePopList(page, condition);
        return IResponse.success(list);
    }

    @ApiOperation(value = "根据合同号码查询保险信息")
    @PostMapping("/getCustInsuranceInfo")
    @Transactional(rollbackFor = Exception.class)
    public IResponse getCustInsuranceInfo(@RequestParam("contractNo") String contractNo) {
        BasicInsuranceInfo basicInsuranceInfo = basicInsuranceInfoService.getOne(Wrappers.<BasicInsuranceInfo>lambdaQuery().
                eq(BasicInsuranceInfo::getContractNo, contractNo)
                .eq(BasicInsuranceInfo::getInsuranceType, AfsEnumUtil.key(InsuranceTypeEnum.BUSINESS)));
        return new IResponse().setData(basicInsuranceInfo);
    }

    @ApiOperation(value = "更新保险信息")
    @PostMapping("/saveBasicInsurance")
    @Transactional(rollbackFor = Exception.class)
    public IResponse<Boolean> saveBasicInsurance(@RequestBody InsuranceRenewInfo renewInfo) {
        List<BasicInsuranceInfo> list = basicInsuranceInfoService.list(Wrappers.<BasicInsuranceInfo>lambdaQuery().
                eq(BasicInsuranceInfo::getContractNo, renewInfo.getContractNo()));
        for (BasicInsuranceInfo insuranceInfo : list) {
            if (InsuranceTypeEnum.BUSINESS == insuranceInfo.getInsuranceType()) {
                insuranceInfo.setInsuranceCompany(renewInfo.getInsureCompanyCom());
                insuranceInfo.setInsuranceMode(renewInfo.getInsureMode());
                insuranceInfo.setInsuranceNo(renewInfo.getInsureNumCom());
                insuranceInfo.setInsuranceStartTime(renewInfo.getInsuranceStartTimeCom());
                insuranceInfo.setInsuranceEndTime(renewInfo.getInsuranceEndTimeCom());
                basicInsuranceInfoService.updateById(insuranceInfo);
            } else if (InsuranceTypeEnum.COMPULSORY == insuranceInfo.getInsuranceType()) {
                insuranceInfo.setInsuranceCompany(renewInfo.getInsureCompanyTph());
                insuranceInfo.setInsuranceMode(renewInfo.getInsureMode());
                insuranceInfo.setInsuranceNo(renewInfo.getInsureNumTph());
                insuranceInfo.setInsuranceStartTime(renewInfo.getInsuranceStartTimeTph());
                insuranceInfo.setInsuranceEndTime(renewInfo.getInsuranceEndTimeTph());
                basicInsuranceInfoService.updateById(insuranceInfo);
            }
        }
        return new IResponse().setData(true);
    }
}
